<?php
/**
 * @file			chkData.class.php
 * @CopyRight		(C)1996-2099 SINA Inc.
 * @Project			自游网
 * @Author			liujz <jianzhou@staff.sina.com.cn>
 * @Create Date:	2011-12-29
 * @Modified By:	jianzhou/2011-12-29
 * @Brief			API 检查数据
 */

class chkData_class 
{
	function checkEmail($email, $errno, $msg='',$throwException=true) 
	{
		if( !preg_match("/^[a-z0-9\-_\.]*[a-z0-9]@(?:[a-z0-9-]+)(?:\.[a-z0-9-]+)+$/i",$email) ){
			$throwException && vApi::throwException($errno, $msg?$msg:'邮箱格式不正确');
			return FALSE;
		}
		return TRUE;
	}
	
	function checkPassword($password, $errno) 
	{
		if( !preg_match("/^[\w\-_~`!@#$%^&*()+={}\[\]|\\:;\"'<>?,.\/]{6,16}$/i", $password) ){
			vApi::throwException($errno, '密码由6~16位字符组成');
		}
	}
	
	
	/**
	 * 昵称检查
	 * @param $nickname
	 * @param $errno
	 */
	function checkNickname($nickname, $errno)
	{
		if(isset($errno['num']) && is_numeric($nickname)){
			vApi::throwException($errno['num'], '不能全是数字，改一下吧');
		}
		$len = mb_strwidth($nickname,'UTF-8');
		if(isset($errno['length']) && ($len < 4 || $len >20) ){
			vApi::throwException($errno['length'], '昵称字符长度范围4-20');
		}
		if( isset($errno['format']) && !preg_match("/^[a-z0-9\-_\x{4e00}-\x{9fa5}]{2,20}$/iu", $nickname)){
			vApi::throwException($errno['format'], '昵称非法');
		}
		
		if( isset($errno['exist']) && vApi::getBase('user')->getUidByNick($nickname) ){
			vApi::throwException($errno['exist'], '昵称已存在');
		}
	}
	
	
	/**
	 * 性别检查，可用 1，2，F,M，默认是女,0:未知
	 * @param $gender
	 */
	function chkGender($gender, $from='')
	{
		$g = ('renren'==$from) ? array('1'=>1,'0'=>2) : array('1'=>'1','2'=>'2','f'=>'2','m'=>'1');
		return isset($g[$gender]) ? $g[$gender] : '0';
	}
	
	
	// 检查是否为空 
	function chkEmpty($val, $errno, $errmsg)
	{
		if ( empty($val) ) { vApi::throwException($errno, $errmsg); }
		return TRUE;
	}
}